package com.mstore.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.mstore.pojo.Account;
import com.mstore.pojo.ExchangeGood;
import com.mstore.pojo.ExchangeLog;
import com.mstore.pojo.Member;
import com.mstore.pojo.Shop;
import com.mstore.pojo.User;
import com.mstore.service.ExchangeGoodsService;
import com.mstore.service.ExchangeLogsService;
import com.mstore.service.MemberService;
import com.mstore.service.ShopService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 兑换产品
 */
public class ExchangeAction extends BaseAction {

	private static final long serialVersionUID = 6261507697684431065L;

	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	private ShopService shopService;

	private ExchangeGoodsService exchangeGoodsService;

	private MemberService memberService;

	private ExchangeGood exchangeGood;

	private Shop shop;

	private ExchangeLog exchangeLog;

	private Member member;

	private List<Shop> shops;

	private List<ExchangeGood> exchangeGoods;

	private List<ExchangeLog> exchangeLogs;

	private List<ExchangeGood> exchanges;

	private List<ExchangeGood> exchangeGoolLog;

	private Long shopId;

	private Long eaId;

	private String goodname;

	private Long exgoodId;

	private String mobile;

	private String exgood;

	private String result;

	private int index;

	private int page;

	private int line;

	private String iscore;

	private List<String> shopIds;

	private String shopIdArray;

	private ExchangeLogsService exchangeLogsService;
	
	private Long memberId;

	private static Logger logger = Logger.getLogger(ExchangeAction.class
			.getName());

	/**
	 * 跳转到添加商品页面
	 * 
	 * @return
	 */
	public String initAddExgood() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;

		shop = new Shop();
		shop.setEaId(account.getId());

		shops = shopService.getAllShop(shop);

		exchangeGood = new ExchangeGood();
		exchangeGood.setEaId(account.getId());

		return "toAddExgoods";
	}

	/**
	 * 添加兑换商品
	 * 
	 * @return
	 */
	public String addExgood() {
		String createTime = super.getCreateTime();
		for (String shopId : shopIds) {
			exchangeGood.setShopId(Long.parseLong(shopId));
			exchangeGood.setCreateTime(createTime);
			exchangeGood.setScore(new Double(iscore));
			if (exchangeGood.getExchangeAmount() == null) {
				exchangeGood.setExchangeAmount(0);
			}
			exchangeGoodsService.addExgood(exchangeGood);
		}

		return "addSuccess";
	}

	/**
	 * 查询全部兑换商品
	 * 
	 * @return
	 */
	public String watchExgoods() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;

		// 查询企业的店铺
		shop = new Shop();
		shop.setEaId(account.getId());
		shops = shopService.getAllShop(shop);

		// 查询企业全部店铺兑换商品
		exchangeGood = new ExchangeGood();
		exchangeGood.setEaId(account.getId());
		exchangeGoods = exchangeGoodsService.getAllExgoods(exchangeGood);

		for (ExchangeGood exchangeGood : exchangeGoods) {
			Shop ishop = new Shop();
			ishop.setId(exchangeGood.getShopId());
			ishop = shopService.getShopById(ishop);
			exchangeGood.setShop(ishop);
		}

		// 隐藏参数eaid、shopId
		eaId = account.getId();
		shopId = 0L;

		return "toWatchExgoods";
	}

	/**
	 * 通过店铺查询兑换商品
	 * 
	 * @return
	 */
	public String watchExgoodsByShop() {
		if (shop.getId() == 0) {
			return "gotoWatchExgoods";
		}

		// 查询企业的店铺
		shop.setEaId(eaId);
		shops = shopService.getAllShop(shop);

		// 查询兑换商品
		exchangeGood = new ExchangeGood();
		exchangeGood.setEaId(eaId);
		exchangeGood.setShopId(shop.getId());

		exchangeGoods = exchangeGoodsService
				.getAllExgoodsByShopId(exchangeGood);

		for (ExchangeGood exchangeGood : exchangeGoods) {
			Shop ishop = new Shop();
			ishop.setId(exchangeGood.getShopId());
			ishop = shopService.getShopById(ishop);
			exchangeGood.setShop(ishop);
		}

		// 隐藏参数shopId
		shopId = shop.getId();

		return "toWatchExgoodsByShop";
	}

	/**
	 * 验证商品名称是否重复
	 * 
	 * @return
	 */
	public String verifyGoodname() {
		Map<String, Object> map = new HashMap<String, Object>();
		exchangeGood = new ExchangeGood();
		if (StringUtils.isBlank(shopIdArray)) {
			map.put("temp", "系统数据异常");
			result = super.mapTojson(map);
			return "returnVerify";
		}
		String[] shopIds = shopIdArray.split(",");
		for (String shopId : shopIds) {
			exchangeGood.setShopId(Long.parseLong(shopId));
			exchangeGood.setEaId(eaId);
			exchangeGood.setGoodname(goodname);
			int count = exchangeGoodsService.verifyExgoodsName(exchangeGood);
			if (count > 0) {
				map.put("temp", "商品名称已存在");
				result = super.mapTojson(map);
			}
		}

		return "returnVerify";
	}

	/**
	 * 修改时验证商品名称是否重复
	 * 
	 * @return
	 */
	public String verifyGoodnameById() {
		exchangeGood = new ExchangeGood();
		exchangeGood.setShopId(shopId);
		exchangeGood.setEaId(eaId);
		exchangeGood.setGoodname(goodname);
		exchangeGood.setId(exgoodId);
		int count = exchangeGoodsService.verifyGoodnameById(exchangeGood);
		if (count > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("temp", "商品名称已存在");
			result = super.mapTojson(map);
		}
		return "returnVerify";
	}

	/**
	 * 删除商品
	 * 
	 * @return
	 */
	public String delExgood() {
		exchangeGoodsService.delExgood(exchangeGood);
		return "delSuccess";
	}

	/**
	 * 跳转到修改页面
	 * 
	 * @return
	 */
	public String initModifyExgood() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;

		shop = new Shop();
		shop.setEaId(account.getId());
		shops = shopService.getAllShop(shop);

		exchangeGood = exchangeGoodsService.getExgoodById(exchangeGood);

		return "toModifyExgood";
	}

	/**
	 * 修改商品
	 * 
	 * @return
	 */
	public String modifyExgood() {
		exchangeGood.setScore(new Double(iscore));
		if (exchangeGood.getExchangeAmount() == null) {
			exchangeGood.setExchangeAmount(0);
		}
		exchangeGoodsService.updateExgood(exchangeGood);
		return "modifySuccess";
	}

	/**
	 * 兑换记录查询
	 * 
	 * @return
	 */
	public String watchExLog() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;

		exchangeLog = new ExchangeLog();
		exchangeLog.setEaId(account.getId());

		line = exchangeGoodsService.getExLogCount(exchangeLog);

		if (line == 0) {
			line = 1;
		}
		if (line % 14 == 0) {
			page = line / 14;
		}
		else {
			page = (line / 14) + 1;
		}

		if (index < 1) {
			index = 1;
		}
		else if (index > page) {
			index = page;
		}

		exchangeLogs = exchangeGoodsService.getAllExLogPage(index, exchangeLog);

		// 填充店铺、兑换商品
		for (ExchangeLog exchangeLog : exchangeLogs) {
			Member member = new Member();
			member.setId(exchangeLog.getMemberId());
			member = memberService.getMemberById(member);
			exchangeLog.setMember(member);

			Shop shop = new Shop();
			shop.setId(exchangeLog.getShopId());
			shop = shopService.getShopById(shop);
			exchangeLog.setShop(shop);

			ExchangeGood exchangeGood = new ExchangeGood();
			exchangeGood.setId(exchangeLog.getGoodId());
			exchangeGood = exchangeGoodsService.getExgoodById(exchangeGood);
			exchangeLog.setExchangeGood(exchangeGood);
		}

		return "toWatchExLog";
	}

	/**
	 * 通过手机号查询兑换记录
	 * 
	 * @return
	 */
	public String watchExLogByTel() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}

		mobile = member.getMobile();

		Account account = (Account) object;

		member.setEaId(account.getId());
		member = memberService.getMemberByMobileAndType(member);

		exchangeLog = new ExchangeLog();
		exchangeLog.setEaId(account.getId());

		if (member != null) {
			exchangeLog.setMemberId(member.getId());
		}

		line = exchangeGoodsService.getExLogCountByMemberId(exchangeLog);

		if (line == 0) {
			line = 1;
		}
		if (line % 14 == 0) {
			page = line / 14;
		}
		else {
			page = (line / 14) + 1;
		}

		if (index < 1) {
			index = 1;
		}
		else if (index > page) {
			index = page;
		}

		exchangeLogs = exchangeGoodsService.getExLogPageByMemberId(index,
				exchangeLog);

		for (ExchangeLog exchangeLog : exchangeLogs) {
			exchangeLog.setMember(member);

			Shop shop = new Shop();
			shop.setId(exchangeLog.getShopId());
			shop = shopService.getShopById(shop);
			exchangeLog.setShop(shop);

			ExchangeGood exchangeGood = new ExchangeGood();
			exchangeGood.setId(exchangeLog.getGoodId());
			exchangeGood = exchangeGoodsService.getExgoodById(exchangeGood);
			exchangeLog.setExchangeGood(exchangeGood);
		}

		return "toWatchExLogByTel";
	}

	/**
	 * 兑换商品库存查询
	 * 
	 * @return
	 */
	public String watchExchangeGoods() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常！";
			return "error";
		}
		Account account = (Account) object;
		Long eaId = account.getId();
		
		if (exchangeGood == null) {
			exchangeGood = new ExchangeGood();
		}
		
		exchangeGood.setEaId(eaId);
		
		int pagetemp = exchangeGoodsService.getCountExgGoodsByCondition(exchangeGood);
		settingPageAndIndex(pagetemp);
		
		exchangeGoods = exchangeGoodsService.getPageExgGoodsByCondition(index, exchangeGood);
		Shop sp = new Shop();
		for (ExchangeGood good : exchangeGoods) {
			sp.setId(good.getShopId());
			sp = shopService.getShopById(sp);
			good.setShop(sp);
		}
		
		// 查询企业店铺，用于在页面上做搜索条件
		sp.setEaId(eaId);
		shops = shopService.getAllShopByEaId(sp);

		return "watchExLogByGood";
	}

	/**
	 * 分店查看兑换库存商品
	 * 
	 * @return
	 */
	public String selectShopExchangeGoods() {

		User user = (User) session.get("user");
		Shop shop = new Shop();
		shop.setId(user.getShopId());
		shop = shopService.getShopById(shop);
		exchangeLog = new ExchangeLog();
		exchangeGood = new ExchangeGood();
		exchangeGood.setShopId(shop.getId());
		exchangeGood.setEaId(shop.getEaId());
		exchangeGoolLog = new ArrayList<ExchangeGood>();
		exchangeLogs = new ArrayList<ExchangeLog>();
		exchangeGoolLog = exchangeGoodsService.getAllExgoodList(exchangeGood);
		if (exgood == null || exgood.equals("0") || exgood.equals("")) {
			line = exchangeGoodsService.getAllExgoodListPageTotal(exchangeGood);
			if (line == 0) {
				line = 1;

			}
			if (line % 14 == 0) {
				page = line / 14;
			}
			else {
				page = (line / 14) + 1;
			}

			if (index < 1) {
				index = 1;
			}
			else if (index > page) {
				index = page;
			}
			exchangeLogs = exchangeGoodsService.getAllExgoodListPage(
					exchangeGood, index);

		}
		else {
			long exgoodId = Long.valueOf(exgood);
			exchangeGood.setId(exgoodId);

			exchangeGoods = exchangeGoodsService
					.getExgoodsByGoodname(exchangeGood);
			// for (int i = 0; i < exchangeGoods.size(); i++) {
			// exchangeLog.setShopId(shop.getId());
			// exchangeLog.setEaId(shop.getEaId());
			// exchangeLog.setGoodId(exchangeGoods.get(i).getId());
			exchangeLog.setShopId(exchangeGoods.get(0).getShopId());
			exchangeLog.setEaId(exchangeGoods.get(0).getEaId());
			exchangeLog.setGoodId(exchangeGoods.get(0).getId());
			line = exchangeGoodsService
					.getExgoodsByGoodidPageTotal(exchangeLog);
			if (line == 0) {
				line = 1;
			}
			if (line % 14 == 0) {
				page = line / 14;
			}
			else {
				page = (line / 14) + 1;
			}

			if (index < 1) {
				index = 1;
			}
			else if (index > page) {
				index = page;
			}
			exchangeLogs = exchangeGoodsService.getExgoodsByGoodidPage(
					exchangeLog, index);
			// }
			logger.info("exchangeGood=" + exchangeGood);
			logger.info("index=" + index);
			logger.info("exchangeLogs=" + exchangeLogs);
		}
		for (ExchangeLog exchangeLog : exchangeLogs) {
			Member member = new Member();
			member.setId(exchangeLog.getMemberId());
			member = memberService.getMemberById(member);
			exchangeLog.setMember(member);

			shop.setId(exchangeLog.getShopId());
			shop = shopService.getShopById(shop);
			exchangeLog.setShop(shop);

			ExchangeGood exchangeGood = new ExchangeGood();
			exchangeGood.setId(exchangeLog.getGoodId());
			exchangeGood = exchangeGoodsService.getExgoodById(exchangeGood);
			exchangeLog.setExchangeGood(exchangeGood);
		}

		return "selectShopExchangeGoods";
	}

	/**
	 * 检查会员兑换同一件商品的次数
	 */
	public String checkExchangeAmount() {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = exchangeLogsService.getCountMemberExchangeLogByGoodId(exgoodId, memberId);
		exchangeGood = new ExchangeGood();
		exchangeGood.setId(exgoodId);
		exchangeGood = exchangeGoodsService.getExgoodById(exchangeGood);
		int exchangeAmount = exchangeGood.getExchangeAmount();
		if (exchangeAmount == 0) {
			map.put("temp", "ok");
			result = super.mapTojson(map);
		}else {
			if (count >= exchangeAmount) {
				map.put("temp", "error");
				map.put("count", exchangeAmount);
				result = super.mapTojson(map);
			}else {
				map.put("temp", "ok");
				result = super.mapTojson(map);
			}
		}
		
		return "checkExchangeAmount";
	}
	
	/**
	 * 设置当前页和总页数
	 */
	private void settingPageAndIndex(int pagetemp) {
		if (pagetemp == 0) {
			pagetemp = 1;
		}
		if (pagetemp % 14 == 0) {
			page = pagetemp / 14;
		}
		else {
			page = (pagetemp / 14) + 1;
		}
		if (index < 1) {
			index = 1;
		}
		else if (index > page) {
			index = page;
		}
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<Shop> getShops() {
		return shops;
	}

	public void setShops(List<Shop> shops) {
		this.shops = shops;
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public ExchangeGood getExchangeGood() {
		return exchangeGood;
	}

	public void setExchangeGood(ExchangeGood exchangeGood) {
		this.exchangeGood = exchangeGood;
	}

	public List<ExchangeGood> getExchangeGoods() {
		return exchangeGoods;
	}

	public void setExchangeGoods(List<ExchangeGood> exchangeGoods) {
		this.exchangeGoods = exchangeGoods;
	}

	public ExchangeGoodsService getExchangeGoodsService() {
		return exchangeGoodsService;
	}

	public void setExchangeGoodsService(
			ExchangeGoodsService exchangeGoodsService) {
		this.exchangeGoodsService = exchangeGoodsService;
	}

	public Long getShopId() {
		return shopId;
	}

	public void setShopId(Long shopId) {
		this.shopId = shopId;
	}

	public Long getEaId() {
		return eaId;
	}

	public void setEaId(Long eaId) {
		this.eaId = eaId;
	}

	public String getGoodname() {
		return goodname;
	}

	public void setGoodname(String goodname) {
		this.goodname = goodname;
	}

	public Long getExgoodId() {
		return exgoodId;
	}

	public void setExgoodId(Long exgoodId) {
		this.exgoodId = exgoodId;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public List<ExchangeLog> getExchangeLogs() {
		return exchangeLogs;
	}

	public void setExchangeLogs(List<ExchangeLog> exchangeLogs) {
		this.exchangeLogs = exchangeLogs;
	}

	public ExchangeLog getExchangeLog() {
		return exchangeLog;
	}

	public void setExchangeLog(ExchangeLog exchangeLog) {
		this.exchangeLog = exchangeLog;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLine() {
		return line;
	}

	public void setLine(int line) {
		this.line = line;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getIscore() {
		return iscore;
	}

	public void setIscore(String iscore) {
		this.iscore = iscore;
	}

	public List<String> getShopIds() {
		return shopIds;
	}

	public void setShopIds(List<String> shopIds) {
		this.shopIds = shopIds;
	}

	public String getExgood() {
		return exgood;
	}

	public void setExgood(String exgood) {
		this.exgood = exgood;
	}

	public List<ExchangeGood> getExchanges() {
		return exchanges;
	}

	public void setExchanges(List<ExchangeGood> exchanges) {
		this.exchanges = exchanges;
	}

	public List<ExchangeGood> getExchangeGoolLog() {
		return exchangeGoolLog;
	}

	public void setExchangeGoolLog(List<ExchangeGood> exchangeGoolLog) {
		this.exchangeGoolLog = exchangeGoolLog;
	}

	public String getShopIdArray() {
		return shopIdArray;
	}

	public void setShopIdArray(String shopIdArray) {
		this.shopIdArray = shopIdArray;
	}

	public ExchangeLogsService getExchangeLogsService() {
		return exchangeLogsService;
	}

	public void setExchangeLogsService(ExchangeLogsService exchangeLogsService) {
		this.exchangeLogsService = exchangeLogsService;
	}

	public Long getMemberId() {
		return memberId;
	}

	public void setMemberId(Long memberId) {
		this.memberId = memberId;
	}

}
